package neusoft.yuejian_11.activity;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import java.util.List;

import neusoft.yuejian_11.adapter.TrainListAdapter;
import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.TrainBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.TrainModel;
import neusoft.yuejian_11.R;

public class TrainSearchActivity extends BaseActivity implements RetrofitListener {
    private EditText keywordView;
    private ImageButton imageButtonSearch;
    private ImageButton imageButtonBack;
    private RecyclerView recyclerView;
    private TrainListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_search);

        initViews();//初始化
        initEvents();//点击事件处理
    }

    private void initViews() {
        keywordView = (EditText) findViewById(R.id.editText_keyword);
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        imageButtonSearch = (ImageButton) findViewById(R.id.imageButtom_search);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    }

    private void initEvents() {
        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        imageButtonSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String keyword  = keywordView.getText().toString();
                searchTrain(keyword);
            }
        });
    }

    private void searchTrain(String trainName) {
        TrainModel trainModel = new TrainModel();
        trainModel.trainSearch(trainName,this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        if (TrainSearchActivity.this !=null) {
            List<TrainBean> trainBeanList = (List<TrainBean>) o;
            adapter = new TrainListAdapter(TrainSearchActivity.this, trainBeanList);
            LinearLayoutManager layoutManager = new LinearLayoutManager(TrainSearchActivity.this);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(adapter);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
        }
    }

    @Override
    public void onFail() {

    }

}
